package org.hfchunya.mutualHelp.baidu;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.hfchunya.mutualHelp.baidu.ueditor.define.ActionMap;
import org.hfchunya.mutualHelp.baidu.ueditor.define.BaseState;
import org.hfchunya.mutualHelp.baidu.ueditor.define.State;
import org.hfchunya.mutualHelp.core.file.model.FileUploadBean;
import org.hfchunya.mutualHelp.core.file.service.FileUploadService;
import org.hfchunya.mutualHelp.core.util.StrUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;

import static org.hfchunya.mutualHelp.core.file.service.FileUploadService.rebuildNoDecode;

/**
 * <p>文件处理</p>
 * @author MGary
 *
 */
@Slf4j
@Service
public class UeditorService {

    /**
     * 文件上传
     */
    @Autowired
    private FileUploadService fileUploadService;

    /**
     * <p> 文件上传</p>
     *
     * @param actionCode    -文件类型编号  <br/>
     * @param outJsonString - ueditor上传返回JSON字符串 <br/>
     * @param actionCode    - 上下文路径 <br/>
     * @param fileAddress    - 文件存储根路径 <br/>
     * @return <br/>
     * @throws
     * @date 2019/2/27 13:05 <br/>
     */
    public String uploadHandle(int actionCode, String outJsonString, String contextPath,String fileAddress) {
        switch (actionCode) {

            case ActionMap.CONFIG:
                break;

            case ActionMap.UPLOAD_IMAGE:
            case ActionMap.UPLOAD_SCRAWL:
            case ActionMap.UPLOAD_VIDEO:
            case ActionMap.UPLOAD_FILE:
                return uploadFileHandler(outJsonString, contextPath,fileAddress);

            case ActionMap.CATCH_IMAGE:
                break;

            case ActionMap.LIST_IMAGE:
            case ActionMap.LIST_FILE:
                break;

        }

        // 没做任何处理
        return outJsonString;
    }

    /**
     * <p>文件上传 </p>
     *
     * @param outJsonString -  ueditor上传返回JSON字符串<br/>
     * @param contextPath - 上下文路径 <br/>
     * @return <br/>
     * @throws
     * @date 2019/2/27 13:07 <br/>
     */
    protected String uploadFileHandler(String outJsonString, String contextPath,String fileAddress) {
        State state = null;
        String preContextPath = StrUtils.isEmpty(contextPath) ? "" : contextPath;
        try {
            //判断文件是否为空
            JSONObject json = JSON.parseObject(outJsonString);
            if (!"SUCCESS".equals(json.getString("state"))) {
                return null;
            }
            // 获取站点路径,正则去空
            String url =  json.getString("url").replace(preContextPath, "");

            String newPath = rebuildNoDecode(fileAddress+url);

            //去除文件名称
            newPath = newPath.replace(json.getString("title"), "");
            if (newPath.endsWith("/")) {
                newPath = newPath.substring(0, newPath.length() - 1);
            }
            // 原路径
            String oldPath = rebuildNoDecode(fileAddress+url);
            // 文件处理
            FileUploadBean bean = fileUploadService.uploadHandle(newPath, new File(oldPath),url);

            if (bean != null) {
                state = new BaseState(true);
                state.putInfo("size", json.getString("size"));
                state.putInfo("title", bean.getName());
                state.putInfo("url", bean.getFileAddress());
                state.putInfo("type", json.getString("type"));
                state.putInfo("original", json.getString("original"));
            } else {
                log.error("UeditorService uploadHandle fail , bean is null.");
            }

        } catch (Exception e) {
            log.error("UeditorService uploadHandle fail.", e);
        }
        return state == null ? outJsonString : state.toJSONString();
    }
}
